home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 January / macformat-020.iso / Shareware City / Developers / AIFF_DSP_v15 folder / AIFF_DSP / sinegen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-07-31  |  1.6 KB  |  78 lines  |  [TEXT/KAHL]

  1. /*
  2. FILE:    sinegen.c
  3. PROJECT: Ford grant DSP
  4. AUTHOR:  Ben Denckla
  5. COMMENT: generates a sine wave of user-specified frequency
  6. LINKAGE: needs sintab.c
  7. */
  8.  
  9. #include "aiff.h"
  10. #include "sintab.h"
  11.  
  12. #include <stdio.h>
  13. #include <math.h>  // must come before SANE #include to have precedence
  14. #include <SANE.h>
  15.  
  16. #define GETNUM( prompt, fmtstr, x ) { \
  17.     int e; \
  18.     do { \
  19.         printf( prompt ); \
  20.         e = !scanf( fmtstr, &x ) || x < 0; \
  21.         fflush( stdin ); \
  22.         if ( e ) puts( "Try again: input was bad." ); \
  23.     } while ( e ); \
  24. }
  25.  
  26. int take_input = 0, make_output = 1;
  27.  
  28. static short harm;
  29.  
  30. // asks user for frequency & length of file
  31. DEFFUNC( void getusrdat( void ) ) {
  32.     GETNUM( "number of samples: ", "%ld", ba.com.fram )
  33.     GETNUM( "sampling rate: ",     "%lf", ph.rate )
  34. }
  35.  
  36. // sets AIFF header info (all else is default)
  37. DEFFUNC( void set_vals( void ) ) {
  38.     ba.com.chan = 1;
  39.     // ba.com.fram already set in getusrdat()
  40.     ba.com.wdsi = 16;
  41.     x96tox80( &(ph.rate), &(ba.com.rate) );// convert float formats (1)
  42.     ba.frmhd.si += 2*ba.com.fram;          // see aiff.c for initial sizes
  43.     ba.sndhd.si += 2*ba.com.fram;          // " " " "
  44.     
  45.     ph.framsiz = 2;
  46. }
  47. // 1. rate already set in getusrdat()
  48.  
  49.  
  50. void init_process( void ) {
  51.     get_sintab();        // get a 1st-quadrant sine wave table
  52.     getusrdat();
  53.     harm = getusrharm();
  54.     set_vals();
  55. }
  56.  
  57. void term_process( void ) {
  58.     // no termination needed
  59. }
  60.  
  61. void process_samdat( long buflen ) {
  62.     long i;         
  63.     register short *td, *endd, tphase, tharm;
  64.     static short phase = 0;
  65.     
  66.     td     = d;
  67.     tphase = phase;
  68.     tharm  = harm;
  69.     endd   = &td[buflen];
  70.  
  71.     do {
  72.         *td++ = sin_tab( tphase &= TABMASK );
  73.         tphase += tharm;
  74.     } while ( td < endd );
  75.     
  76.     phase = tphase;
  77. }
  78.